package com.hnichr.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hnichr.pojo.Plate;
import com.hnichr.service.StopService;
import com.hnichr.share.*;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class StopServiceImpl implements StopService {

    private static final Logger logger = LoggerFactory.getLogger(StopServiceImpl.class);

    @Autowired
    private MqttClient mqttClient;

    public String snapshot(){
        MqttMessage message = new MqttMessage();
        message.setId(1234567898);
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("id","NYGtiXpPy5ratyzU");
        dataMap.put("sn","b956ba98-988cd83e");
        dataMap.put("name","snapshot");
        dataMap.put("version","1.0");
        dataMap.put("timestamp",String.valueOf(new Date().getTime()));

        Map<String,Object> payloadMap = new HashMap<>();
        payloadMap.put("type","snapshot");
        payloadMap.put("body",new HashMap<>());
        dataMap.put("payload",payloadMap);
        message.setPayload(JSON.toJSONString(dataMap).getBytes());
        String imagePath=null;
        try{
            mqttClient.publish("b956ba98-988cd83e/device/message/down/snapshot", message);
            synchronized (SnapshotShare.getInstance()){
                SnapshotShare.getInstance().wait();
                imagePath = SnapshotShare.getInstance().getImagePath();
            }
        }
        catch (Exception e){
            logger.error("消息投递失败");
        }
        return imagePath;
    }

    public String open(){
        MqttMessage message = new MqttMessage();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("id","NYGtiXpPy5ratyzU");
        dataMap.put("sn","b956ba98-988cd83e");
        dataMap.put("name","gpio_out");
        dataMap.put("version","1.0");
        dataMap.put("timestamp",String.valueOf(new Date().getTime()));

        Map<String,Object> payloadMap = new HashMap<>();
        payloadMap.put("type","gpio_out");
        Map<String,Object> bodyMap = new HashMap<>();
        bodyMap.put("delay",500);
        bodyMap.put("io",0);  //[0,3]   //对应的输出 IO 编号，取值范围为[0,3]之间的整数
        bodyMap.put("value",2);  //0 断, 1 通 ,2 先通后断
        payloadMap.put("body",bodyMap);
        dataMap.put("payload",payloadMap);
        message.setPayload(JSON.toJSONString(dataMap).getBytes());
        String code = null;
        try{
            mqttClient.publish("b956ba98-988cd83e/device/message/down/gpio_out", message);
            synchronized (OpenShare.getInstance()){
                OpenShare.getInstance().wait();
                code = OpenShare.getInstance().getCode();
            }
        }
        catch (Exception e){
            logger.error("消息投递失败",e);
            return "失败";
        }
        return "200".equals(code)?"成功":"失败";
    }

    public List<Plate> writeListQuery(String plate){
        MqttMessage message = new MqttMessage();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("id","Zr7O1PzBPD57Q4ku");
        dataMap.put("sn","b956ba98-988cd83e");
        dataMap.put("name","white_list_operator");
        dataMap.put("version","1.0");
        dataMap.put("timestamp",String.valueOf(new Date().getTime()));

        Map<String,Object> payloadMap = new HashMap<>();
        payloadMap.put("type","white_list_operator");
        Map<String,Object> bodyMap = new HashMap<>();
        bodyMap.put("operator_type","select");
        bodyMap.put("plate",plate);
        bodyMap.put("sub_type","plate");
        payloadMap.put("body",bodyMap);
        dataMap.put("payload",payloadMap);
        message.setPayload(JSON.toJSONString(dataMap).getBytes());
        try{
            mqttClient.publish("b956ba98-988cd83e/device/message/down/white_list_operator", message);
            synchronized (WhitelistQueryShare.getInstance()){
                WhitelistQueryShare.getInstance().wait();
                return WhitelistQueryShare.getInstance().getPlateList();
            }
        }
        catch (Exception e){
            logger.error("消息投递失败");
        }
        return new ArrayList<>();
    }

    public String writeListAddOrUpdate(String plate,String context,String enableTime,String overdueTime,int enable){
        MqttMessage message = new MqttMessage();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("id","Zr7O1PzBPD57Q4ku");
        dataMap.put("sn","b956ba98-988cd83e");
        dataMap.put("name","white_list_operator");
        dataMap.put("version","1.0");
        dataMap.put("timestamp",String.valueOf(new Date().getTime()));

        Map<String,Object> payloadMap = new HashMap<>();
        payloadMap.put("type","white_list_operator");
        Map<String,Object> bodyMap = new HashMap<>();
        bodyMap.put("operator_type","update_or_add");
        Map<String,Object> dldcMap = new HashMap<>();
        dldcMap.put("create_time", DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        dldcMap.put("enable_time", enableTime);
        dldcMap.put("overdue_time", overdueTime);
        dldcMap.put("enable", enable);
        dldcMap.put("plate",plate);
        dldcMap.put("time_seg_enable",0);
        dldcMap.put("seg_time","");
        dldcMap.put("need_alarm",0);
        dldcMap.put("vehicle_code","");
        dldcMap.put("vehicle_comment",context);
        dldcMap.put("customer_id","");
        bodyMap.put("dldb_rec",dldcMap);
        payloadMap.put("body",bodyMap);
        dataMap.put("payload",payloadMap);
        message.setPayload(JSON.toJSONString(dataMap).getBytes());
        String code = null;
        try{
            mqttClient.publish("b956ba98-988cd83e/device/message/down/white_list_operator", message);
            synchronized (WhitelistAddOrDeleteShare.getInstance()){
                WhitelistAddOrDeleteShare.getInstance().wait();
                code = WhitelistAddOrDeleteShare.getInstance().getCode();
            }
        }
        catch (Exception e){
            logger.error("消息投递失败");
            return "失败";
        }
        return "200".equals(code)?"成功":"失败";
    }

    public String writeListDel(String plate){
        MqttMessage message = new MqttMessage();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("id","Zr7O1PzBPD57Q4ku");
        dataMap.put("sn","b956ba98-988cd83e");
        dataMap.put("name","white_list_operator");
        dataMap.put("version","1.0");
        dataMap.put("timestamp",String.valueOf(new Date().getTime()));

        Map<String,Object> payloadMap = new HashMap<>();
        payloadMap.put("type","white_list_operator");
        Map<String,Object> bodyMap = new HashMap<>();
        bodyMap.put("operator_type","delete");
        bodyMap.put("plate",plate);
        payloadMap.put("body",bodyMap);
        dataMap.put("payload",payloadMap);
        message.setPayload(JSON.toJSONString(dataMap).getBytes());
        String code = null;
        try{
            mqttClient.publish("b956ba98-988cd83e/device/message/down/white_list_operator", message);
            synchronized (WhitelistAddOrDeleteShare.getInstance()){
                WhitelistAddOrDeleteShare.getInstance().wait();
                code = WhitelistAddOrDeleteShare.getInstance().getCode();
            }
        }
        catch (Exception e){
            logger.error("消息投递失败");
            return "失败";
        }
        return "200".equals(code)?"成功":"失败";
    }

    public String ioStatus(String type,String gpio){
        MqttMessage message = new MqttMessage();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("id","NYGtiXpPy5ratyzU");
        dataMap.put("sn","b956ba98-988cd83e");
        dataMap.put("name","get_io_status");
        dataMap.put("version","1.0");
        dataMap.put("timestamp",String.valueOf(new Date().getTime()));

        Map<String,Object> payloadMap = new HashMap<>();
        payloadMap.put("type","get_io_status");
        Map<String,Object> bodyMap = new HashMap<>();
        bodyMap.put("type",type);
        bodyMap.put("gpio",gpio);
        payloadMap.put("body",bodyMap);
        dataMap.put("payload",payloadMap);
        message.setPayload(JSON.toJSONString(dataMap).getBytes());
        String code = null;
        try{
            mqttClient.publish("b956ba98-988cd83e/device/message/down/get_io_status", message);
            synchronized (IOStatusShare.getInstance()){
                IOStatusShare.getInstance().wait();
                return "status:"+IOStatusShare.getInstance().getStatus()+"  gpio:"+IOStatusShare.getInstance().getGpio();
            }
        }
        catch (Exception e){
            logger.error("消息投递失败");
        }
        return "失败";
    }
}
